﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.RDS
{
    /// <summary>
    /// 枚举数据表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("RDS", "EnumerateTable", "1.0")]
    public class EnumerateTableAction : BaseAction<EnumerateTableParams, EnumerateTableResult>
    {
        protected override ActionResult<EnumerateTableResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            EnumerateTableParams opParams)
        {
            int limit = 15;
            int start = opParams.Page * limit;
            SER.DtSerService dtSerService = new SER.DtSerService();
            ListResult<TDataSource> serResult = dtSerService.EnumerateDataSources(opParams.DataServiceId);
            if (serResult.IsSuccess)
            {
                EnumerateTableResult result = new EnumerateTableResult();
                result.Total = serResult.Data.Count;
                result.Page = opParams.Page;
                result.Limit = limit;
                result.Start = opParams.Page * limit;

                if(!string.IsNullOrEmpty(opParams.Filter))
                {
                    List<TDataSource> sources = serResult.Data.Where(p => p.Name.Contains(opParams.Filter)).Skip(start).Take(limit).ToList();

                    result.Count = sources.Count;
                    for (int index = 0; index < sources.Count; index++)
                    {
                        EnumerateTableResult.Item item = new EnumerateTableResult.Item();
                        item.Name = sources[index].Name;
                        item.Id = sources[index].Value;

                        result.Items.Add(item);
                    }
                    return new ActionResult<EnumerateTableResult>(result);
                }
                else
                {
                    List<TDataSource> sources = serResult.Data.Skip(start).Take(limit).ToList();

                    result.Count = sources.Count;
                    for (int index = 0; index < sources.Count; index++)
                    {
                        EnumerateTableResult.Item item = new EnumerateTableResult.Item();
                        item.Name = sources[index].Name;
                        item.Id = sources[index].Value;

                        result.Items.Add(item);
                    }
                    return new ActionResult<EnumerateTableResult>(result);
                }
            }
            else
            {
                return new ActionResult<EnumerateTableResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class EnumerateTableParams
    {
        public Guid DataServiceId { get; set; }
        public int Page { get; set; }
        public string Filter { get; set; }
    }

    public class EnumerateTableResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public string Name { get; set; }
            public string Id { get; set; }
        }
    }
}
